<?php
$loader->requireOnce('includes/LabResult/LabResultProcessor.class.php');
$loader->requireOnce('includes/LabAssignment/LabAssignmentProcessor.class.php');

/**
 * Controler for processing lab result.
 * @package modules.dept_laboratory.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_LabResult extends Controller {
	private $logger ;
	private $labResultProcessor;
	function __construct($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'LabResult') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'patient';
		}
		$this->logger = LoggerManager::getLogger('module.dept_laboratory.controllers.C_LabResult');
		$this->template_mod = $template_mod;
		$this->view->path="lab_result";
		$this->labResultProcessor = new LabResultProcessor($this);
	}

	/**
	 * For input lab result.
	 * @return unknown_type
	 */
	public function actionViewInputLabResultPage() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$labAssignmentTicketObj->setup($labAssignmentTicketId);
		$person = Celini::newORDO('Person', $labAssignmentTicketObj->get('patient_id'));
		$patient = Celini::newORDO('Patient', $labAssignmentTicketObj->get('patient_id'));
		$this->assign('patientName', $person->get('name'));
		$this->assign('patientId', $labAssignmentTicketObj->get('patient_id'));
		$this->assign('patientCode', $patient->get('patient_code'));
		$this->assign('patientDob', $person->getDateofBirth());
		$this->assign('patientAge', $person->getAge());
		$this->assign('selectedlabAssignmentTicketId', $labAssignmentTicketId);
		$this->assign('selectedLabTicketId', $selectedLabTicketId);
		$this->assign('labAssignmentTicketList', '');
		$this->assign('labTicketList', '');
		$html = '';
		$html = $this->view->render('lab_result.html');
		return $html;
	}
	
    /**
     * For input lab result.
     * @return unknown_type
     */
    public function actionPrintLabResultPage($labAssignmentTicketId = 0) {
    	if (isset($_GET['labAssignmentTicketId'])) {
            $labAssignmentTicketId = $_GET['labAssignmentTicketId'];
    	}
        //$selectedLabTicketId = $_GET['selectedLabTicketId'];
        $html = '';
        $result = $this->labResultProcessor->renderAssignmentResultPage4Print($labAssignmentTicketId);
        $this->assign('meta', $result['meta']);
        $this->assign('resultContent', $result['resultContent']);
        $html = $this->view->render('lab_result_print.html');
        return $html;
    }
	/**
	 * For view lab result only.
	 * @return unknown_type
	 */
	public function viewLabResultPage($ticketId) {
		
		$labAssignmentPro = new LabAssignmentProcessor(null);
        $html = '';
        if ( $labAssignmentPro->countAllLabAssignmentByTicketId($ticketId) == 0) {
            $html = "<div class='no_result' style='margin-top: 10px;'><span class='label'>"
            . L10nHelper::_("The patient has no laboratory assignment")
            . "</span></div>";
        } else {

            $ticketObj = Celini::newOrdo('Ticket');
            $ticketObj->setup($ticketId);
            $person = Celini::newORDO('Person', $ticketObj->getPatientId());
            $patient = Celini::newORDO('Patient', $ticketObj->getPatientId());
            $this->assign('patientName', $person->get('name'));
            $this->assign('ticketId', $ticketId);
            $this->assign('patientId', $ticketObj->getPatientId());
            $this->assign('patientCode', $patient->get('patient_code'));
            $this->assign('patientDob', $person->getDateofBirth());
            $this->assign('patientAge', $person->getAge());
            $this->assign('selectedlabAssignmentTicketId', '');
            $this->assign('selectedLabTicketId', '');
            $this->assign('labAssignmentTicketList', '');
            $this->assign('labTicketList', '');
            $html = '';
            $html = $this->view->render('lab_result_view_only.html');
        }
        return $html;
	}
    /**
     * Render result part for input result
     * @return unknown_type
     */
	public function actionRenderLabResultInputAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$this->logger->info('Start actionRenderLabResultInputAjax');
		$result = array(
            'error' =>  array(
            	'code'  =>  CoreErrorCode::NORMAL,
                'mess'  =>  ''
            ),
            'result'=>  ''
        );
		try {
      
            $result['result'] = $this->labResultProcessor->renderAssignmentInputResultPage($labAssignmentTicketId, $selectedLabTicketId);

		} catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionRenderLabResultInputAjax fail. Error detail: '. $ex->getMessage());
		} catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionRenderLabResultInputAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error('actionRenderLabResultInputAjax fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionRenderLabResultInputAjax');
		return json_encode($result);
	}
    /**
     * Render result part for view the result
     * @return unknown_type
     */
	public function actionRenderLabResult4ViewAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$this->logger->info('Start actionRenderLabResult4ViewAjax');
		$result = array(
            'error' =>  array(
                    'code'  =>  CoreErrorCode::NORMAL,
                    'mess'  =>  ''
                    ),
            'result'=>  ''
            );
            try {

            	$result['result'] = $this->labResultProcessor->renderAssignmentResultPage4View($labAssignmentTicketId, $selectedLabTicketId);

            } catch (EnumException $ex) {
            	$result['error']['code'] = $ex->getErrorCode();
            	$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            	$this->logger->error('actionRenderLabResult4ViewAjax fail. Error detail: '. $ex->getMessage());
            }
            catch (LabException $ex) {
            	$result['error']['code'] = $ex->getErrorCode();
            	$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            	$this->logger->error('actionRenderLabResult4ViewAjax fail. Error detail: '. $ex->getMessage());
            } catch (Exception $ex) {
            	$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            	$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            	$this->logger->error('actionRenderLabResult4ViewAjax fail. Error detail: '. $ex->getMessage());

            }
            $this->logger->info('Finish actionRenderLabResult4ViewAjax');
            return json_encode($result);
	}
	/**
	 * Render assignment ticket list for select which lab assigment to
	 * input result
	 * @return unknown_type
	 */
	public function actionRenderSelectorAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$patientId = $_GET['patientId'];
		$this->logger->info('Start actionrenderSelectorAjax');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		try {
			$result['result']=$this->labResultProcessor->renderSelectorHtml($patientId,
			$labAssignmentTicketId,
			$selectedLabTicketId);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionrenderSelectorAjax fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionrenderSelectorAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('actionrenderSelectorAjax fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionrenderSelectorAjax');
		return json_encode($result);
	}

	public function actionRenderSelector4ViewOnlyAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$ticketId = $_GET['ticketId'];
		$this->logger->info('Start actionRenderSelector4ViewOnlyAjax');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		try {
			
			$result['result']=$this->labResultProcessor->renderSelectorHtml4View($ticketId,
			$labAssignmentTicketId,
			$selectedLabTicketId);
			

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderSelector4ViewOnlyAjax fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderSelector4ViewOnlyAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('actionRenderSelector4ViewOnlyAjax fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionRenderSelector4ViewOnlyAjax');
		return json_encode($result);
	}
	/**
	 * render LabTicket list allow user to select with lab ticket to
	 * input result
	 * @return unknown_type
	 */

	public function actionRenderLabTicketSelectorAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$this->logger->info('Start actionRenderLabTicketSelectorAjax');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		try {
			$result['result']=$this->labResultProcessor->renderLabTicketSelectorHtml($labAssignmentTicketId,
			$selectedLabTicketId,
			true);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderLabTicketSelectorAjax fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderLabTicketSelectorAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('actionRenderLabTicketSelectorAjax fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionRenderLabTicketSelectorAjax');
		return json_encode($result);
	}

	public function actionRenderLabTicketSelector4ViewAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$this->logger->info('Start actionRenderLabTicketSelector4ViewAjax');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		try {
			$result['result']=$this->labResultProcessor->renderLabTicketSelectorHtml($labAssignmentTicketId,
			$selectedLabTicketId,
			false);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderLabTicketSelector4ViewAjax fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderLabTicketSelector4ViewAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('actionRenderLabTicketSelector4ViewAjax fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionRenderLabTicketSelector4ViewAjax');
		return json_encode($result);
	}

	/**
	 * Save lab result.
	 * @return unknown_type
	 */

	public function actionSaveLabResultAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$selectedLabTicketId = $_GET['selectedLabTicketId'];
		$resultData = $_GET['resultData'];
		$labResultId = $_GET['labResultId'];
		$metaData = array();
		$metaData['technician1_id'] = $_GET['technicianId1'];
		$metaData['technician2_id'] = $_GET['technicianId2'];
		$employeeObj1 = Celini::newOrdo('Employee', $_GET['technicianId1']);
		$employeeObj2 = Celini::newOrdo('Employee', $_GET['technicianId2']);
		$metaData['name1'] = $employeeObj1->getName();
		$metaData['name2'] = $employeeObj2->getName();
		$metaData['machine_name'] = $_GET['machine'];
		$metaData['is_done'] = true;
		$this->logger->info('Start actionSaveLabResultAjax');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		try {
			$resultDataArr = array();
			$len = count($labResultId);
			for ($index = 0; $index < $len; $index++) {
				$resultDataArr[$labResultId[$index]] =   $resultData[$index];
			}
			 
			$result['result']=$this->labResultProcessor->saveLabResult($selectedLabTicketId,$metaData, $resultDataArr);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionSaveLabResultAjax fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionSaveLabResultAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('actionSaveLabResultAjax fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionSaveLabResultAjax');
		return json_encode($result);

	}
}
?>